/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CompradoresVentana.java
 *
 * Created on 22/11/2010, 15:01:23
 */

package ar.com.educacionit.vehiculos.ventanas;

import ar.com.educacionit.vehiculos.entidades.Comprador;
import ar.com.educacionit.vehiculos.entidades.Vendedor;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

/**
 *
 * @author matias
 */
public class CompradoresVentana extends javax.swing.JInternalFrame {
    public boolean nuevo= true;
    DefaultListModel modeloComp= new DefaultListModel();
    private VentanaMaestra vm;

    public void limpiarCampos(){
        jTfNombre.setText("");
        jTfApellido.setText("");
        jTfDNI.setText("");
        jTfPresupuesto.setText("");
        jTfNombre.requestFocus();
    }

    public boolean camposCompletos(){
        if(jTfNombre.getText().equals("")){
                JOptionPane.showMessageDialog(this,
                        "El Nombre no puede estar incompleto",
                        "Error",
                        JOptionPane.WARNING_MESSAGE);
                jTfNombre.requestFocus();
                return false;
        }
        if(jTfApellido.getText().equals("")){
                    JOptionPane.showMessageDialog(this,
                        "El Apellido no puede estar incompleto",
                        "Error",
                        JOptionPane.WARNING_MESSAGE);
                jTfApellido.requestFocus();
                return false;
                }
        if(jTfDNI.getText().equals("")){
                    JOptionPane.showMessageDialog(this,
                        "El DNI no puede estar incompleto",
                        "Error",
                        JOptionPane.WARNING_MESSAGE);
                jTfDNI.requestFocus();
                return false;
                }
        if(jTfPresupuesto.getText().equals("")){
                    JOptionPane.showMessageDialog(this,
                        "El Presupuesto no puede estar incompleto",
                        "Error",
                        JOptionPane.WARNING_MESSAGE);
                jTfPresupuesto.requestFocus();
                return false;
                }
        return true;
    }

    public boolean validarCampos(){
        try{
            double numero= Double.parseDouble(jTfPresupuesto.getText());
        }catch(Exception e){
            JOptionPane.showMessageDialog(this,
                        "El Presupuesto debe ser un numero con decimales",
                        "Error",
                        JOptionPane.WARNING_MESSAGE);
                jTfPresupuesto.requestFocus();
                return false;
        }
        return true;
    }

    public void grabarDb(){
        JOptionPane.showMessageDialog(this,
                        "El cromprador fue guardado con éxito",
                        "Mensaje",
                        JOptionPane.INFORMATION_MESSAGE);

            Comprador c= new Comprador();
            if(jListComp.getSelectedIndex()!=-1){
                c= (Comprador) jListComp.getSelectedValue();
                nuevo=false;
            }
            c.setNombre(jTfNombre.getText());
            c.setApellido(jTfApellido.getText());
            c.setNumeroDocumento(jTfDNI.getText());
            c.setPresupuesto(Double.parseDouble(jTfPresupuesto.getText()));
        try{
            if(nuevo){
                modeloComp.addElement(c);
                c.insertar(vm.getConnection());
                jListComp.clearSelection();
            }else{
                c.modificar(vm.getConnection());
            }
        }catch(Exception e){

            }
            limpiarCampos();
            jButtonNuevo.setEnabled(true);
            jButtonGuardar.setEnabled(false);
    }

    public void cargarLista(){
        Comprador c= new Comprador();
        try{
            ArrayList cList=  c.obtenerTodos(vm.getConnection());
            Iterator itC= cList.iterator();

            while(itC.hasNext()){
                Comprador comp= (Comprador) itC.next();
                modeloComp.addElement(comp);
            }
        }catch(Exception e){
            
        }
    }

    public CompradoresVentana(VentanaMaestra vm) {
        this.vm=vm;
        initComponents();
        cargarLista();
        jListComp.setModel(modeloComp);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTfApellido = new javax.swing.JTextField();
        jTfNombre = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jListComp = new javax.swing.JList();
        jTfPresupuesto = new javax.swing.JTextField();
        jTfDNI = new javax.swing.JTextField();
        jButtonCerrar = new javax.swing.JButton();
        jButtonEliminar = new javax.swing.JButton();
        jButtonGuardar = new javax.swing.JButton();
        jButtonNuevo = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Administración de Compradores");
        setEnabled(false);
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameClosed(evt);
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameOpened(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Ubuntu", 1, 13));
        jLabel4.setText("Presupuesto");

        jLabel3.setFont(new java.awt.Font("Ubuntu", 1, 13));
        jLabel3.setText("DNI");

        jLabel2.setFont(new java.awt.Font("Ubuntu", 1, 13));
        jLabel2.setText("Apellido");

        jLabel1.setFont(new java.awt.Font("Ubuntu", 1, 13));
        jLabel1.setText("Nombre");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Lista de compradores", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Dialog", 1, 12))); // NOI18N

        jListComp.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jListCompMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jListComp);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 192, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 202, Short.MAX_VALUE)
                .addContainerGap())
        );

        jButtonCerrar.setFont(new java.awt.Font("Ubuntu", 1, 13));
        jButtonCerrar.setText("Cerrar");
        jButtonCerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCerrarActionPerformed(evt);
            }
        });

        jButtonEliminar.setFont(new java.awt.Font("Ubuntu", 1, 13)); // NOI18N
        jButtonEliminar.setText("Eliminar");
        jButtonEliminar.setEnabled(false);
        jButtonEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEliminarActionPerformed(evt);
            }
        });

        jButtonGuardar.setFont(new java.awt.Font("Ubuntu", 1, 13)); // NOI18N
        jButtonGuardar.setText("Guardar");
        jButtonGuardar.setEnabled(false);
        jButtonGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonGuardarActionPerformed(evt);
            }
        });

        jButtonNuevo.setFont(new java.awt.Font("Ubuntu", 1, 13));
        jButtonNuevo.setText("Nuevo");
        jButtonNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonNuevoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 543, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jTfPresupuesto, javax.swing.GroupLayout.DEFAULT_SIZE, 197, Short.MAX_VALUE)
                                    .addComponent(jTfNombre, javax.swing.GroupLayout.DEFAULT_SIZE, 197, Short.MAX_VALUE)
                                    .addComponent(jTfApellido, javax.swing.GroupLayout.DEFAULT_SIZE, 197, Short.MAX_VALUE)
                                    .addComponent(jTfDNI, javax.swing.GroupLayout.DEFAULT_SIZE, 197, Short.MAX_VALUE))))
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButtonNuevo, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButtonGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 185, Short.MAX_VALUE)
                        .addComponent(jButtonCerrar, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(17, 17, 17))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(33, 33, 33)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(jTfNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jTfApellido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(jTfDNI, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(jTfPresupuesto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonNuevo)
                    .addComponent(jButtonGuardar)
                    .addComponent(jButtonEliminar)
                    .addComponent(jButtonCerrar))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formInternalFrameClosed(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameClosed
         vm.getjMenuAbrirComp().setEnabled(true);
    }//GEN-LAST:event_formInternalFrameClosed

    private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened
         vm.getjMenuAbrirComp().setEnabled(false);
    }//GEN-LAST:event_formInternalFrameOpened

    private void jButtonCerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCerrarActionPerformed
        this.dispose();
    }//GEN-LAST:event_jButtonCerrarActionPerformed

    private void jButtonNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonNuevoActionPerformed
        limpiarCampos();
        jButtonNuevo.setEnabled(false);        
        jButtonGuardar.setEnabled(true);
    }//GEN-LAST:event_jButtonNuevoActionPerformed

    private void jButtonGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonGuardarActionPerformed
        if(camposCompletos()){
            if(validarCampos()){
                grabarDb();
                jListComp.updateUI();
            }
        }
    }//GEN-LAST:event_jButtonGuardarActionPerformed

    private void jListCompMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jListCompMouseClicked
        jButtonGuardar.setEnabled(true);
        jButtonEliminar.setEnabled(true);

        Comprador com= (Comprador) jListComp.getSelectedValue();
        jTfNombre.setText(com.getNombre());
        jTfApellido.setText(com.getApellido());
        jTfDNI.setText(com.getNumeroDocumento());
        jTfPresupuesto.setText(String.valueOf(com.getPresupuesto()));
    }//GEN-LAST:event_jListCompMouseClicked

    private void jButtonEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEliminarActionPerformed
        try {
            int rta = JOptionPane.showConfirmDialog(this, "Desea eliminar el comprador " + jListComp.getSelectedValue() + " ?", "Eliminar", JOptionPane.YES_NO_OPTION);
            if (rta == JOptionPane.NO_OPTION) {
                return;
            }
            Comprador c = (Comprador) jListComp.getSelectedValue();
            modeloComp.remove(jListComp.getSelectedIndex());
            c.eliminar(vm.getConnection());
            jButtonGuardar.setEnabled(false);
            jButtonEliminar.setEnabled(false);
            jButtonNuevo.setEnabled(true);
            limpiarCampos();
        } catch (Exception ex) {
            
        }
    }//GEN-LAST:event_jButtonEliminarActionPerformed

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCerrar;
    private javax.swing.JButton jButtonEliminar;
    private javax.swing.JButton jButtonGuardar;
    private javax.swing.JButton jButtonNuevo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JList jListComp;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField jTfApellido;
    private javax.swing.JTextField jTfDNI;
    private javax.swing.JTextField jTfNombre;
    private javax.swing.JTextField jTfPresupuesto;
    // End of variables declaration//GEN-END:variables

}
